<template>
    <div class="container">



        <!-- 基本搜索+功能按钮 -->
        <el-form ref="searchForm1" :inline="true" :model="searchForm" size="medium" label-width="">
            <div class="table_tool_bar_wrap">
                <div class="base_wrap mb10">
                    <div class="left">
                        <el-form-item label="会员类型" prop="memberType">
                            <el-radio-group v-model="searchForm.memberType" @change="handleChangeMemberType" size="small">
                                <el-radio-button label="">全部</el-radio-button>
                                <el-radio-button :label="1">普通会员</el-radio-button>
                                <el-radio-button :label="2">分销员</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </div>
                </div>

                <div class="base_wrap mb10">
                    <div class="left">
                        <el-form-item label="快捷搜索" prop="keyword">
                            <el-input placeholder="" v-model="searchForm.keyword" clearable size="small"/>
                        </el-form-item>
                        <el-button @click="handleSearch" type="primary" size="small">搜索</el-button>
                        <el-button @click="handleReset" type="info" plain size="small">重置</el-button>
                        <el-button type="text" size="medium" @click="searchFlag = !searchFlag">
                            高级搜索<i class="el-icon--right" :class="searchFlag?'el-icon-arrow-down':'el-icon-arrow-up'"></i>
                        </el-button>
                    </div>
                </div>
            </div>
        </el-form>

        <!-- 高级搜索 -->
        <el-form class="mt10" ref="searchForm2" :inline="true" :model="searchForm" v-if="!searchFlag" size="medium" label-width="">
            <div class="table_tool_bar_wrap">
                <div class="base_wrap mb10">
                    <div class="left">
                        <el-form-item label="账户余额">
                            <el-input v-model="searchForm.totalAmountMin" type="number" @input="numberic(searchForm.data,'totalAmountMin',$event)" style="width:100px;"></el-input>
                            -
                            <el-input v-model="searchForm.totalAmountMax" type="number" @input="numberic(searchForm.data,'totalAmountMax',$event)" style="width:100px;"></el-input>
                        </el-form-item>
                        <el-form-item label="可提现金额区间">
                            <el-input v-model="searchForm.takeAmountMin" type="number" @input="numberic(searchForm.data,'takeAmountMin',$event)" style="width:100px;"></el-input>
                            -
                            <el-input v-model="searchForm.takeAmountMax" type="number" @input="numberic(searchForm.data,'takeAmountMax',$event)" style="width:100px;"></el-input>
                        </el-form-item>
                        <el-form-item label="待入账金额区间">
                            <el-input v-model="searchForm.freezeAmountMin" type="number" @input="numberic(searchForm.data,'freezeAmountMin',$event)" style="width:100px;"></el-input>
                            -
                            <el-input v-model="searchForm.freezeAmountMax" type="number" @input="numberic(searchForm.data,'freezeAmountMax',$event)" style="width:100px;"></el-input>
                        </el-form-item>
                    </div>
                </div>
            </div>
        </el-form>

        <!-- 表格 -->
        <el-table
            class="mt10"
            ref="table"
            size="medium"
            height="500"
            empty-text="暂无数据"
            element-loading-text="给我一点时间"
            fit border highlight-current-row
            show-summary
            :summary-method="getSummaries"
            :stripe="stripe"
            v-loading="loading"
            :data="dataList"
            @selection-change="handleSelection">

            <el-table-column type="selection" width="55" prop="selection" v-if="isHideColumn('selection')" align="center"></el-table-column>
            <el-table-column label="序号" type="index" prop="index" v-if="isHideColumn('index')" width="60" align="center" />
            <el-table-column label="会员名称" prop="name" v-if="isHideColumn('name')" align="left" show-overflow-tooltip/>
            <el-table-column label="手机号" prop="phone" v-if="isHideColumn('phone')" align="left" show-overflow-tooltip/>
            <el-table-column label="账户余额" prop="amount" v-if="isHideColumn('totalAmount')" align="left" show-overflow-tooltip/>

        </el-table>

        <!-- 分页 -->
        <TablePagination
            :total.sync="total"
            :page.sync="searchForm.page"
            :limit.sync="searchForm.limit"
            @handleSizeChange="handleSizeChange"
            @handleCurrentChange="handleCurrentChange"/>

        <!-- 弹窗, 新增 / 修改 -->
        <AddOrUpdate ref="addOrUpdate" @refreshDataList="getDataList"></AddOrUpdate>

    </div>
</template>

<script>
import TableMixin from '@/mixin/TableMixin'
import TableTools from '@/views/components/table/table-tools'
import TablePagination from '@/views/components/table/table-pagination'
import AddOrUpdate from './add-or-update-dialog'
import { numberic, numberic2, numberic100 } from "@/libs";
export default {
    mixins: [TableMixin],
    components: {
        TableTools,
        TablePagination,
        AddOrUpdate
    },

    data() {
        return {
            dataList: [],
            total: 0,
            loading: false,
            drawerVisible: false,
            selectionRows: [],
            searchFlag: true,
            searchForm: {
                page: 1,
                limit: 10,
                sortColumns: null,
                sortType: null,
                keyword: null,
                openid: null,
                memberId: null,
                name: null,
                phone: null,
                memberType: '',
                totalAmountMin: null,
                totalAmountMax: null,
                takeAmountMin: null,
                takeAmountMax: null,
                freezeAmountMin: null,
                freezeAmountMax: null,
            },

            numberic, numberic2, numberic100

        }
    },

    created() {
    },

    mounted() {
        this.$nextTick(() => {
            this.getDataList()
        })
    },

    methods: {

        /**
         * 侧边栏抽屉打开
         */
        handleDrawerVisible(row) {
            this.detail = row
            this.drawerVisible = true
        },

        /**
         * 新增或编辑
         */
        handleAddOrUpdateVisible(id) {
            // 表单窗口
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id)
            })
        },

        /**
         * 删除
         */
        async handleDelete(row) {
            let ids = []
            if (row) {
                ids.push(row.id)
            }
            if(this.selectionRows) {
                this.selectionRows.filter((item, index) => {
                    ids.push(item.id)
                })
            }
            if (ids.length <= 0) {
                this.$notify({ title: '提示', message: '请选择需要删除的记录！', type: 'info' });
            } else {
                const confirm = await this.$confirm('确定要删除？', '提示', { type: 'warning' }).catch(() => {})
                if(confirm) {
                    const res = await this.$http({
                        url: '/mall/fmsmemberwallet/delete',
                        method: 'POST',
                        data: ids
                    })
                    if (this.ResultMessage(res)) {
                        this.getDataList()
                    }
                }
            }
        },

        /**
         * 获取表格数据
         */
        async getDataList() {
            this.loading = true
            const res = await this.$http({
                url: '/mall/fmsmemberwallet/page',
                method: 'GET',
                params: this.searchForm
            })
            if (this.ResultMessage(res, false)) {
                this.dataList = res.data.records || []
                this.total = res.data.total
            }
            this.loading = false
        },

        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (column.property === 'phone') {
                    sums[index] = '总价';
                    return;
                }
                const values = data.map(
                    item =>
                    column.property === 'totalAmount' ||
                    column.property === 'takeAmount' ||
                    column.property === 'applyTakeAmount' ||
                    column.property === 'commissionAmount' ||
                    column.property === 'factTakeAmount' ? Number(item[column.property]) : 0
                );
                if (!values.every(value => isNaN(value))
                    && (
                        column.property === 'totalAmount' ||
                        column.property === 'takeAmount' ||
                        column.property === 'applyTakeAmount' ||
                        column.property === 'commissionAmount' ||
                        column.property === 'factTakeAmount'
                    )) {

                    let sumTotal = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        if (!isNaN(value)) {
                            return prev + curr;
                        } else {
                            return prev;
                        }
                    }, 0);

                    sums[index] = sumTotal.toFixed(2) + ' 元'
                } else {
                    sums[index] = '';
                }
            });
            this.$nextTick(() => {
                this.$refs.table.doLayout();
            });
            return sums;
        },

        handleChangeMemberType() {
            this.getDataList()
        },


        /**
         * 同意申请窗口
         */
        handleAgreeVisible(row) {
            this.agree.info = row
            this.agree.data.id = row.id
            this.agree.dialogVisible = true
        },

        /**
         * 同意申请
         */
        async handleAgree() {
            const valid = await this.$refs.agree.validate().catch(() => {})
            console.log(valid);
            if(valid) {
                const confirm = await this.$confirm('确认拒绝收货？', '提示', { type: 'warning' }).catch(() => {})
                if(confirm) {
                    const res = await this.$http({
                        url: '/mall/omsorderreturnapply/rejectGoods',
                        method: 'POST',
                        data: this.agree.data
                    })
                    if (this.ResultMessage(res)) {
                        this.agree.dialogVisible = false
                        this.getDataList()
                    }
                }
            }
        },

        /**
         * 拒绝申请窗口
         */
        handleRejectVisible(row) {
            this.reject.info = row
            this.reject.data.id = row.id
            this.reject.dialogVisible = true
        },

        /**
         * 拒绝申请
         */
        async handleReject() {
            const confirm = await this.$confirm('确认拒绝收货？', '提示', { type: 'warning' }).catch(() => {})
            if(confirm) {
                const res = await this.$http({
                    url: '/mall/omsorderreturnapply/rejectGoods',
                    method: 'POST',
                    data: this.reject.data
                })
                if (this.ResultMessage(res)) {
                    this.reject.dialogVisible = false
                    this.getDataList()
                }
            }
        },

        /**
         * 搜索
         */
        handleSearch() {
            this.getDataList()
        },
        /**
         * 重新加载数据表格
         */
        handleReload() {
            this.getDataList()
        },
        /**
         * 重置搜索表单
         */
        handleReset() {
            if(this.$refs.searchForm1) {
                this.$refs.searchForm1.resetFields()
            }
            if(this.$refs.searchForm2) {
                this.$refs.searchForm2.resetFields()
            }

            this.searchForm.memberType = ''
            this.searchForm.totalAmountMin = null
            this.searchForm.totalAmountMax = null
            this.searchForm.takeAmountMin = null
            this.searchForm.takeAmountMax = null
            this.searchForm.freezeAmountMin = null
            this.searchForm.freezeAmountMax = null
        }

    }

}
</script>

<style lang="scss" scoped>

</style>
